package mn.slide.laborberlin.db;

import java.sql.SQLException;

import mn.slide.laborberlin.model.Anhang;
import mn.slide.laborberlin.model.Eintrag;
import mn.slide.laborberlin.model.History;
import mn.slide.laborberlin.model.Kapitel;
import mn.slide.laborberlin.model.Method;
import mn.slide.laborberlin.model.SubUeberSchrift;
import mn.slide.laborberlin.model.UeberSchrift;
import mn.slide.laborberlin.model.UnterSuchung;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	// name of the database file for your application -- change to something
	// appropriate for your app
	private static final String DATABASE_NAME = "Labor.sqlite";
	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int DATABASE_VERSION = 2;

	public DatabaseHelper(final Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	private Dao<Anhang, Integer> anhangDao = null;
	private Dao<Eintrag, Integer> eintragDao = null;
	private Dao<History, Integer> historyDao = null;
	private Dao<Kapitel, Integer> kapitelDao = null;
	private Dao<Method, Integer> methodDao = null;
	private Dao<SubUeberSchrift, Integer> subUeberSchriftDao = null;
	private Dao<UeberSchrift, Integer> ueberSchriftDao = null;
	private Dao<UnterSuchung, Integer> unterSuchungDao = null;

	@Override
	public void onCreate(final SQLiteDatabase db,
			final ConnectionSource connectionSource) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTableIfNotExists(connectionSource, Anhang.class);
			TableUtils.createTableIfNotExists(connectionSource, Eintrag.class);
			TableUtils.createTableIfNotExists(connectionSource, History.class);
			TableUtils.createTableIfNotExists(connectionSource, Kapitel.class);
			TableUtils.createTableIfNotExists(connectionSource, Method.class);
			TableUtils.createTableIfNotExists(connectionSource,
					SubUeberSchrift.class);
			TableUtils.createTableIfNotExists(connectionSource,
					UeberSchrift.class);
			TableUtils.createTableIfNotExists(connectionSource,
					UnterSuchung.class);
		} catch (final SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade(final SQLiteDatabase db,
			final ConnectionSource connectionSource, final int oldVersion,
			final int newVersion) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, Anhang.class, true);
			TableUtils.dropTable(connectionSource, Eintrag.class, true);
			TableUtils.dropTable(connectionSource, History.class, true);
			TableUtils.dropTable(connectionSource, Kapitel.class, true);
			TableUtils.dropTable(connectionSource, Method.class, true);
			TableUtils.dropTable(connectionSource, SubUeberSchrift.class, true);
			TableUtils.dropTable(connectionSource, UeberSchrift.class, true);
			TableUtils.dropTable(connectionSource, UnterSuchung.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (final SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	public void upgrade() {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, Anhang.class, true);
			TableUtils.dropTable(connectionSource, Eintrag.class, true);
			TableUtils.dropTable(connectionSource, Kapitel.class, true);
			TableUtils.dropTable(connectionSource, SubUeberSchrift.class, true);
			TableUtils.dropTable(connectionSource, UeberSchrift.class, true);
			TableUtils.dropTable(connectionSource, UnterSuchung.class, true);
			// after we drop the old databases, we create the new ones
			TableUtils.createTableIfNotExists(connectionSource, Anhang.class);
			TableUtils.createTableIfNotExists(connectionSource, Eintrag.class);
			TableUtils.createTableIfNotExists(connectionSource, Kapitel.class);
			TableUtils.createTableIfNotExists(connectionSource,
					SubUeberSchrift.class);
			TableUtils.createTableIfNotExists(connectionSource,
					UeberSchrift.class);
			TableUtils.createTableIfNotExists(connectionSource,
					UnterSuchung.class);
		} catch (final SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	public Dao<Anhang, Integer> getAnhangDao() throws SQLException {
		if (this.anhangDao == null) {
			this.anhangDao = getDao(Anhang.class);
		}
		return this.anhangDao;
	}

	public Dao<Eintrag, Integer> getEintragDao() throws SQLException {
		if (this.eintragDao == null) {
			this.eintragDao = getDao(Eintrag.class);
		}
		return this.eintragDao;
	}

	public Dao<History, Integer> getHistoryDao() throws SQLException {
		if (this.historyDao == null) {
			this.historyDao = getDao(History.class);
		}
		return this.historyDao;
	}

	public Dao<Kapitel, Integer> getKapitelDao() throws SQLException {
		if (this.kapitelDao == null) {
			this.kapitelDao = getDao(Kapitel.class);
		}
		return this.kapitelDao;
	}

	public Dao<Method, Integer> getMethodDao() throws SQLException {
		if (this.methodDao == null) {
			this.methodDao = getDao(Method.class);
		}
		return this.methodDao;
	}

	public Dao<SubUeberSchrift, Integer> getSubUeberSchriftDao()
			throws SQLException {
		if (this.subUeberSchriftDao == null) {
			this.subUeberSchriftDao = getDao(SubUeberSchrift.class);
		}
		return this.subUeberSchriftDao;
	}

	public Dao<UeberSchrift, Integer> getUeberSchriftDao() throws SQLException {
		if (this.ueberSchriftDao == null) {
			this.ueberSchriftDao = getDao(UeberSchrift.class);
		}
		return this.ueberSchriftDao;
	}

	public Dao<UnterSuchung, Integer> getUnterSuchungDao() throws SQLException {
		if (this.unterSuchungDao == null) {
			this.unterSuchungDao = getDao(UnterSuchung.class);
		}
		return this.unterSuchungDao;
	}

	@Override
	public void close() {
		super.close();
		this.anhangDao = null;
		this.eintragDao = null;
		this.historyDao = null;
		this.kapitelDao = null;
		this.methodDao = null;
		this.subUeberSchriftDao = null;
		this.ueberSchriftDao = null;
		this.unterSuchungDao = null;
	}

}
